
% allows user to go through the automatic tracking results of
% batchTrackVasodilationVesselFill_v2_0 and reject kymographs that are
% mis-tracked

kymfig = figure;
fn = dir('*preselected2.mat');
n = 0;
redo = false;
while n < length(fn)
    n = n + 1;
    load(fn(n).name)
    if ~redo
        if exist('validtracking')           %prevents re-doing files you already covered
            clear results validtracking
            continue;
        end
    else
        redo = false;
    end
    if isfield(results,'kymos') && ~isempty(results.kymos)
        validtracking = zeros(1,length(results.kymos));
    else
        continue;
    end
    k = 0;
    while k < length(results.kymos) 
        k = k + 1;
        figure(kymfig)
        imagesc(results.kymos{k})
        imtitle = [fn(n).name, ' kymo #',num2str(k)];
        title(imtitle);pause(.1)
        hold on
        if isfield(results,'roughedges') && false
            plot(results.roughedges{k}(1,:),'r.'); plot(results.roughedges{k}(2,:),'g.');
            hold off
        elseif isfield(results,'edgepos')
            plot(results.edgepos{k}(1,:),'r.'); plot(results.edgepos{k}(2,:),'g.');
            hold off
        elseif isfield(results,'edgeImages')
            edgepos{k} = nan(2,size(results.kymos{k},2));
            [a,b] = find(results.edgeImages{k} == 1);
            edgepos{k}(1,b) = a;
            [a,b] = find(results.edgeImages{k} == 2);
            edgepos{k}(2,b) = a;
            plot(edgepos{k}(1,:),'r.'); plot(edgepos{k}(2,:),'g.');
            hold off
        end
        temp = input('is kymograph valid? [1/0] OR go back [-1]: ');
        if isempty(temp)
            temp = input('is kymograph valid? [1/0] OR go back [-1]: ');
        end
        if temp == -1 && k>1
            k = k - 2;
        elseif temp == -1 && k==1
            n = n -2;
            redo = true;
            break;
        else
            validtracking(k) = temp;
        end
    end
    if ~redo
        if isfield(results,'edgeImages')
            save(fn(n).name,'validtracking','edgepos','-append');
        else
            save(fn(n).name,'validtracking','-append');
        end
    end
    clear results validtracking edgepos
end